<template>
  <div class="app-container">
    <div class="header-content">
      <span class="header-title">全局触达配置</span>
      <el-button
        style="float: right"
        type="success"
        plain
        icon="el-icon-edit"
        size="mini"
        @click="handleUpdate"
        v-hasPermi="['system:user:edit']"
      >编辑</el-button>
    </div>

    <el-card class="panel-content">
      <el-row>
        <el-col :span="24">
          <el-row class="row-content">
            <div><img src="../../../assets/images/message.png" class="index-img" width="30" height="30"/>短信</div>
          </el-row>

          <el-row class="row-content-text">
            <el-col :span="14">
              <div><el-radio v-model="smsLimit" label="Y">限制(如下)</el-radio></div>
              <div><span>每个用户 1 自然日内，最多接收 1 条短信</span></div>
            </el-col>
            <el-col :span="8">
              <el-radio v-model="smsLimit" label="N">不限制</el-radio>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="panel-content">
      <el-row>
        <el-col :span="24">
          <el-row class="row-content">
            <div> <img src="../../../assets/images/android.png" class="index-img" width="30" height="30"/>APP 推送</div>
          </el-row>
          <el-row class="row-content-text">
            <el-col :span="14">
              <div><el-radio v-model="appLimit" label="Y">限制(如下)</el-radio></div>
              <div><span>每个用户 1 自然日内，最多接收 3 条 App 推送</span></div>
              <div><span>每个用户 5 小时内，最多接收  1 条 App 推送  小时窗口整点滑动
                <el-tooltip class="item" effect="dark" content="比如设置了 4 小时内最多接收 1 条，那么 0:00-4:00  1:00-5:00 2:00-6:00 期间都是最多接收 1 条，依次类推" placement="top-start">
                  <i class="el-icon-info" style="color: #1c84c6">小时窗口整点滑动</i>
                </el-tooltip>
              </span></div>
            </el-col>
            <el-col :span="8">
              <el-radio v-model="appLimit" label="N">不限制</el-radio>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="panel-content">
      <el-row>
        <el-col :span="24">
          <el-row class="row-content">
            <div> <img src="../../../assets/images/copy-link.png" class="index-img" width="30" height="30"/>Webhook</div>
          </el-row>
          <el-row class="row-content-text">
            <el-col :span="14">
              <div><el-radio v-model="webhookLimit" label="Y">限制(如下)</el-radio></div>
            </el-col>
            <el-col :span="8">
              <el-radio v-model="webhookLimit" label="N">不限制</el-radio>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="panel-content">
      <el-row>
        <el-col :span="24">
          <el-row class="row-content">
            <div> <img src="../../../assets/images/weixin-mini.png" class="index-img" width="30" height="30"/>弹窗</div>
          </el-row>
          <el-row class="row-content-text">
            <el-col :span="14">
              <div><el-radio v-model="windowLimit" label="Y">限制(如下)</el-radio></div>
            </el-col>
            <el-col :span="8">
              <el-radio v-model="windowLimit" label="N">不限制</el-radio>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

    <!-- 添加或修改全局触达配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类编码" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入分类编码" />
        </el-form-item>
        <el-form-item label="分类名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="时间" prop="timeValue">
          <el-input v-model="form.timeValue" placeholder="请输入时间" />
        </el-form-item>
        <el-form-item label="时间类型" prop="timeUnit">
          <el-input v-model="form.timeUnit" placeholder="请输入时间类型" />
        </el-form-item>
        <el-form-item label="限制数量" prop="limitNum">
          <el-input v-model="form.limitNum" placeholder="请输入限制数量" />
        </el-form-item>
        <el-form-item label="是否限制" prop="limitFlag">
          <el-input v-model="form.limitFlag" placeholder="请输入是否限制" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig, exportConfig } from "@/api/baseconfig/config";

export default {
  name: "Config",
  data() {
    return {
      smsLimit:'Y',
      appLimit:'Y',
      webhookLimit:'Y',
      windowLimit:'Y',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 全局触达配置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeCode: null,
        typeName: null,
        timeValue: null,
        timeUnit: null,
        limitNum: null,
        limitFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询全局触达配置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeCode: null,
        typeName: null,
        timeValue: null,
        timeUnit: null,
        limitNum: null,
        limitFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加全局触达配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改全局触达配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除全局触达配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有全局触达配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportConfig(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.app-container {
  background:#f7f9fa;
  padding: 20px;
}
.header-content{
  padding: 10px 24px 20px 12px;
  .header-title{
    font-weight: 600;
    font-size: 24px;
    line-height: 34px;
    color: #1f2d3d;
  }
}
.panel-content{
  box-shadow: 2px 0 7px 0 rgb(153 170 192 / 17%);
  margin-bottom:14px;
}
.row-content{
  justify-content: space-between;
  border-bottom: 1px solid #e9f0f7;
  padding: 0 16px;
  background: #fff;
  div{
    line-height:34px;
    img{
      margin-right:10px;
    }
  }
}
.row-text{
  text-align: center;
  font-size: 16px;
  color: #99a9bf;
  line-height: 28px;
}
.row-content-text{
  padding: 9px 20px 9px 43px;
  div{
    line-height:35px;
    span{
      padding: 0 5px;
      font-size: 14px;
      color: #475669;
    }
  }
}
</style>
